import React, { Component } from "react";

import { Link, Route, Switch } from "react-router-dom";

const Home1 = () => {
  return <div>home1页面</div>;
};

const Home2 = () => {
  return <div>home2页面</div>;
};

class Home extends Component {
  render() {
    // console.log(this.props);
    // const url = this.props.match.url;
    const {
      match: { url },
    } = this.props;
    return (
      <div>
        <div>这是home组件</div>
        <ul>
          <li>
            <Link to={`${url}/home1`}>首页1</Link>
          </li>
          <li>
            <Link to={`${url}/home2`}>首页2</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          <Route path={`${url}/home1`} component={Home1}></Route>
          <Route path={`${url}/home2`} component={Home2}></Route>
        </Switch>
      </div>
    );
  }
}

class About extends Component {
  render() {
    return <div>这是About组件</div>;
  }
}

const Mine = () => {
  return <div>这是我的组件</div>;
};

class App extends Component {
  state = {};
  render() {
    return (
      <>
        <h2>react-router-nest</h2>
        <ul>
          <li>
            <Link to="/home">首页</Link>
          </li>
          <li>
            <Link to="/about">关于</Link>
          </li>
          <li>
            <Link to="/mine">我的</Link>
          </li>
        </ul>

        <hr />

        <Switch>
          {/* 如果一个路由需要做下一级路由的，不能添加exact属性 */}
          <Route path="/home" component={Home}></Route>
          <Route path="/about" component={About}></Route>
          <Route path="/mine" component={Mine}></Route>
        </Switch>
      </>
    );
  }
}

export default App;
